define(function () {
    return function (pane) {
        /**
         * 导航
         */
        {
            pane.html(utils.getHtml('default_table'));

            utils.addFilterGroupbtn(pane, {
                text: '全部',
                name: 'level',
                filters: dicts.user_level
            });

            utils.addFilterGroupbtn(pane, {
                name: 'status',
                filters: dicts.status
            });
        }

        //全局变量
        var fields_arrays = [
            ['level', 'select', '用户级别', false, dicts.user_level],
            ['keyword', 'text', '关键词'],
            ['order_by', 'text', '排序'],
            ['status', 'select', '状态', false, dicts.status],
            ['response', 'textarea', '回复内容']
        ];
        var addFields = utils.convertFields(fields_arrays),
            editFields = utils.convertFields(fields_arrays);

        //初始化工具
        utils.addTools(pane, [
            {className: 'add-response-btn', icon: 'fa-plus', text: '添加自动回复'},
            {className: 'refresh-btn', icon: 'fa-refresh', text: '刷新'}]
        );
        $(pane).on('click', '.add-response-btn', function () {
            utils.formAdd(table, {
                name: '自动回复', fields: addFields, columns: 6,
                fnServerData: function (paramData) {
                    return adminApi('responses_add', paramData);
                }
            });
        }).on('click', '.refresh-btn', function () {
            if (table) {
                table.api().draw();
                analyse();
            }
        });

        //初始化表格
        var table = utils.dataTable(pane, {
            columns_arrays: [
                ["id", "#"],
                ["level", "用户级别", false, utils.dictRender('user_level')],
                ["keyword", "关键词", false],
                ["response", "回复内容", false],
                ["status", "状态", false, utils.dictRender('status')],
                ["created_at", "创建日期", false, utils.dateRender()]
            ],
            sorting: [[0, 'desc']],
            fnServerData: function (paramData) {
                return adminApi('responses_browse', paramData);
            }
        });

        //编辑开关
        var toggleHandler = utils.toggleChild(pane, 0, function (checked) {
            table.api().rows().every(function (idx) {
                var child = this.child, data = this.data();
                if (checked == 1) {
                    var opts = [
                        {text: '编辑自动回复', icon: 'fa-edit', className: 'edit-ope'},
                        {text: '删除自动回复', icon: 'fa-trash-o', className: 'delete-ope btn-danger'}
                    ];
                    child(utils.childWrapper(idx, opts)).show();
                } else {
                    child.hide();
                }
            });
        });

        //初始化事件
        table.on('draw.dt', function () {
            toggleHandler();
        }).on('click', '.edit-ope', utils.tableEvent(table, function (idx, rowData) {
            //编辑
            adminApi('responses_load', {id: rowData.id}).then(function (data) {
                utils.formEdit(table, {
                    idx: idx, name: '', fields: editFields, columns: 6, data: data,
                    fnServerData: function (paramData) {
                        return adminApi('responses_edit', paramData);
                    }
                });
            });
        })).on('click', '.delete-ope', utils.tableEvent(table, function (idx, rowData) {
            //删除
            utils.confirmation(this, {
                placement: 'top',
                optName: '删除自动回复',
                title: '你确定删除自动回复?',
                fnConfirm: function () {
                    return adminApi('responses_delete', {id: rowData.id}).then(utils.handDelete(table, {name: '自动回复'}));
                }
            });
        }));
    }
});